var _DEFAULT_MAP = [
  {
    'cKey': '系统名称',
    'cCode': 'sysTitle',
    'cValue': 'ezjs sails管理系统'
  },
  {
    'cKey': '系统欢迎语',
    'cCode': 'sysWelcome',
    'cValue': '欢迎使用ezjs sails管理系统'
  },
];


module.exports = {


  MAP: {},

  refresh: function () {
    async.forEach(_DEFAULT_MAP, function (it, callback) {
      Constants.findOne({
        where: {
          cCode: it.cCode
        }
      }).exec(function (err, item) {
        if (err) {
          return callback(err);
        }
        if (!item) {
          Constants.create(it).exec(function () {
            ConstantsService.set(it['cCode'], it['cValue']);
            return callback(); // tell async that the iterator has completed
          });
        } else {
          ConstantsService.set(item['cCode'], item['cValue']);
          return callback(); // tell async that the iterator has completed
        }
      });
    }, function (err) {
      //console.log('done');
      //return cb(err);
    });
  },

  removeAll: function(cb) {
    Constants.destroy().exec(cb);
  },

  get: function (code) {
    return ConstantsService.MAP[code];
  },
  set: function (code, val) {
    ConstantsService.MAP[code] = val;
  }
};
